<style lang="scss">
    .address{
        min-height: 100vh;
        background-color: #f5f5f5;
    }
    .notify{
        height: 46px;
    }
</style>
<template>
    <div class="address">
        <p-navigator text="收货地址"/>
        <van-address-list
            v-model="isDefaultId"
            :list="list" 
            @add="add()"
            @select="select"
            @edit="edit"
            default-tag-text="默认"/>
    </div>
</template>
<script>
import {getAddressList,editAddress} from '@/api'
export default {
    methods: {
        getList(){
            getAddressList().then(res => {
                if(res.data.code == 1){
                    this.list = res.data.result.map(e => {
                        // 如果is_select 为 1 时 
                        if(e.is_select == 1){
                            // 让选中的列表ID 赋值
                            this.isDefaultId = e.ID;
                        }
                        e.address_id = e.ID;
                        e.id = e.ID;
                        e.tel = e.phone;
                        e.address= e.a_provice + e.a_city + e.a_code + e.a_desc;
                        e.isDefault =Boolean(e.is_select)
                        return e
                    });
                }
            })
        },
        select(item){
            if(item.is_select != 1){
                item.is_select = 1;
                // 修改资料
                editAddress(item).then(res => {
                    // console.log(res.data);
                    if(res.data.code == 1){
                        this.$notify({ type: 'success', message: '修改成功',className:'notify' });
                        // 重新调用获取地址列表的接口
                        this.getList()
                    }
                })
            }
            
        },
        add(){
            this.$router.push('/addaddress')
        },
        edit(item){
            this.$router.push('/editAddress/'+item.ID)
            // console.log(item);
        }
    },
    created(){
        this.getList();
    },
    data() {
        return {
            isDefaultId: -1,
            list: []
        }
    },
}
</script>